package com.jl.core.controller;

import com.jl.config.BaseResponse;
import com.jl.config.PageModel;
import com.jl.core.model.resp.PopularizationResp;
import com.jl.core.model.resp.ProxyPlayerResp;
import com.jl.core.model.resp.ProxyResp;
import com.jl.core.service.UserProxyService;
import com.jl.core.util.file.FileUtil;
import com.jl.model.resp.ExchangeResp;
import com.jl.model.resp.RebateDetailResp;
import com.jl.model.resp.RebateInfoResp;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("proxy")
@Slf4j
@Api(description = "我的代理")
public class UserProxyController {

    @Autowired
    private UserProxyService userProxyService;

    private static final String pathAndroid = "/root/photo/jinluanlianmengA.apk";
    private static final String pathIos = "/root/photo/aaa.jpg";

    @GetMapping("bindInvCode")
    @ApiOperation(value = "绑定邀请码")
    public BaseResponse bindInvCode(Integer code){
        userProxyService.bindInvCode(code);
        return BaseResponse.success();
    }


    @GetMapping("invCode")
    @ApiOperation(value = "查询邀请码")
    public BaseResponse<String> invCode(){
        return BaseResponse.success(userProxyService.invCode());
    }

    @GetMapping("popularization")
    @ApiOperation(value = "我的推广")
    public BaseResponse<PopularizationResp> popularization(){
        PopularizationResp popularizationResp = userProxyService.popularization();
        return BaseResponse.success(popularizationResp);
    }

    @GetMapping("download")
    @ApiOperation(value = "扫码下载")
    public void download(HttpServletRequest request,HttpServletResponse response) throws Exception {
        UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
        OperatingSystem os = userAgent.getOperatingSystem();
        if ("ANDROID".equals(os.getName())){
            FileUtil.downFile(pathAndroid,response);
        }else if ("IOS".equals(os.getName())){
            FileUtil.downFile(pathIos,response);
        }else {
            FileUtil.downFile(pathAndroid,response);
        }
    }

    @GetMapping("myPlayer")
    @ApiOperation(value = "我的玩家")
    public BaseResponse<ProxyPlayerResp> myPlayer(Integer userId,@RequestParam(defaultValue = "1") Integer pageNumb, @RequestParam(defaultValue = "7") Integer pageSize){
        ProxyPlayerResp proxyPlayerResp = userProxyService.player(userId,pageNumb,pageSize);
        return BaseResponse.success(proxyPlayerResp);
    }

    @GetMapping("upgrade")
    @ApiOperation(value = "升级代理")
    public BaseResponse upgrade(Integer userId,String proxyRate){
        userProxyService.upgrade(userId,proxyRate);
        return BaseResponse.success();
    }


    @GetMapping("myProxy")
    @ApiOperation(value = "我的代理")
    public BaseResponse<ProxyResp> myProxy(Integer userId, @RequestParam(defaultValue = "1") Integer pageNumb, @RequestParam(defaultValue = "7") Integer pageSize){
        ProxyResp proxyResp = userProxyService.myProxy(userId,pageNumb,pageSize);
        return BaseResponse.success(proxyResp);
    }

    @GetMapping("updateRate")
    @ApiOperation(value = "修改比例")
    public BaseResponse<ProxyResp> updateRate(Integer userId,String proxyRate){
        userProxyService.updateRate(userId,proxyRate);
        return BaseResponse.success();
    }

    @GetMapping("rebate")
    @ApiOperation(value = "返利查询")
    public BaseResponse<RebateInfoResp> rebate(Long startDate,Long endDate){
        RebateInfoResp rebateInfoResp = userProxyService.rebate(startDate,endDate);
        return BaseResponse.success(rebateInfoResp);
    }

    @GetMapping("rebateDetail")
    @ApiOperation(value = "返利明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "1玩家返利 2代理返利",required = true, paramType = "query")
    })
    public BaseResponse<PageModel<RebateDetailResp>> rebateDetail(Long startDate, Long endDate,String type,
                                                                  @RequestParam(defaultValue = "1") Integer pageNumb,
                                                                  @RequestParam(defaultValue = "9") Integer pageSize){
        PageModel<RebateDetailResp> pageModel = userProxyService.rebateDetail(startDate,endDate,type,pageNumb,pageSize);
        return BaseResponse.success(pageModel);
    }

    @GetMapping("queryExchange")
    @ApiOperation(value = "兑换查询")
    public BaseResponse<ExchangeResp> queryExchange(@RequestParam(defaultValue = "1") Integer pageNumb, @RequestParam(defaultValue = "4") Integer pageSize){
        ExchangeResp exchange = userProxyService.queryExchange(pageNumb,pageSize);
        return BaseResponse.success(exchange);
    }

    @GetMapping("exchangeCoin")
    @ApiOperation(value = "兑换金币")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rest", value = "剩余金币",required = true, paramType = "query")
    })
    public BaseResponse exchangeCoin(String coin,String rest){
        userProxyService.exchangeCoin(coin,rest);
        return BaseResponse.success();
    }
}
